/*
 * Copyright 2011 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.gaellalire.jzip.macosx;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import com.apple.eawt.AppEvent.OpenFilesEvent;
import com.apple.eawt.AppEvent.QuitEvent;
import com.apple.eawt.Application;
import com.apple.eawt.OpenFilesHandler;
import com.apple.eawt.QuitHandler;
import com.apple.eawt.QuitResponse;

import fr.gaellalire.jzip.macosx.cocoa.NSApplication;
import fr.gaellalire.jzip.macosx.cocoa.ServicesProvider;
import fr.gaellalire.jzip.view.JZipFrame;

/**
 *
 * @author Gael Lalire
 */
@SuppressWarnings("restriction")
public class JZip implements OpenFilesHandler, QuitHandler {

    private List<JZipFrame> frames = new ArrayList<JZipFrame>();

    private ActionListener openListener;

    public JZip() {
        Application application = com.apple.eawt.Application.getApplication();
        JMenuBar menuBar = new JMenuBar();
        JMenu file = new JMenu("Fichier");
        JMenuItem item = new JMenuItem("Ouvrir...");
        item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        openListener = new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                FileDialog fileDialog = new FileDialog((Frame) null);
                fileDialog.setVisible(true);
                File file = new File(fileDialog.getDirectory(), fileDialog.getFile());
                if (file != null) {
                    openFile(file);
                }
            }
        };
        item.addActionListener(openListener);
        file.add(item);
        menuBar.add(file);
        application.setDefaultMenuBar(menuBar);
        application.setOpenFileHandler(this);
        application.setQuitHandler(this);
    }

    public static void main(final String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException,
            InterruptedException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        JZip zip = new JZip();
        final ServicesProvider c = new ServicesProvider();
        NSApplication.sharedApplication().setServicesProvider(c.id());
        synchronized (zip) {
            zip.wait();
        }
    }

    public void openFile(final File file) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                final JZipFrame jZipFrame = new JZipFrame(file, openListener);
                jZipFrame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(final WindowEvent e) {
                        synchronized (frames) {
                            frames.remove(jZipFrame);
                        }
                    }
                });
                jZipFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jZipFrame.setLocationByPlatform(true);
                synchronized (frames) {
                    frames.add(jZipFrame);
                }
                jZipFrame.setVisible(true);
            }
        });
    }

    @Override
    public void openFiles(final OpenFilesEvent openFilesEvent) {
        List<File> files = openFilesEvent.getFiles();
        for (File file : files) {
            openFile(file);
        }
    }

    @Override
    public void handleQuitRequestWith(final QuitEvent quitEvent, final QuitResponse quitResponse) {
        synchronized (frames) {
            for (JZipFrame zipFrame : frames) {
                zipFrame.dispose();
            }
            frames.clear();
        }
        System.exit(0);
    }

}
